#pragma once

#include <ddblk/locks/syslock_int.h>

#include <pthread.h>

typedef struct _mutexlock
{
	pthread_mutex_t lock;
} _mutexlock_t;

DECL_SYSLOCK(mutex)

PARAM_NONULL(1) SAI _mutexlock_t *_mutex_init(void *ptr, size_t size)
{	
	if (size < sizeof(_mutexlock_t)) return NULL;
	_mutexlock_t *s = (_mutexlock_t *)ptr;
	pthread_mutex_init(&s->lock, NULL);
	return s;
}

PARAM_NONULL(1) SAI void *_mutex_destruct(_mutexlock_t *s)
{
	PARAM_USED(s);
	pthread_mutex_destroy(&s->lock);
	return (void *)s;
}

PARAM_NONULL(1) SAI bool_t _mutex_lock(_mutexlock_t *s)
{
	return (!pthread_mutex_lock(&s->lock));
}

PARAM_NONULL(1) SAI bool_t _mutex_unlock(_mutexlock_t *s)
{
	return (!pthread_mutex_unlock(&s->lock));
}

DEF_SYSLOCK(mutex, mutex)
